#include <iostream>
//#include "data_process.h"
#include <deque>
#include "main.h"
#include <unistd.h>
#include <cstdio>
#include "snake_define.h"
#include <pthread.h>

#include <list>
std::list<Snake> test_snake_list;
Snake test_snake_1(NULL, 10, 10, UP, 5);
Snake test_snake_2(NULL, 10, 10, UP, 5);
//std::list<Snake &> test_snake_list_2;

typedef struct Node
{
    int x;
    int y;
}Node;

void show_all_nodes(std::deque<Node> node_list);
void snake_move_test();
//void* snake_move(void* arg);
void* snake_move_thread1(void* arg);
void* snake_move_thread2(void* arg);
void* snake_move_thread3(void* arg);
void* snake_move_thread4(void* arg);

void test1();
void test2();
void test3();
int main()
{
    test1();
    return 0;
}




void test_1()
{
    std::deque<Node> node_list;
    for(int i=0; i<5; i++)
    {
        Node newnode;
        newnode.x = i*10;
        newnode.y = i*20;

        node_list.push_front(newnode);
    }

    show_all_nodes(node_list);
}



void test1()
{
    //snake_move_test();
    test_snake_list.push_back(test_snake_1);
    pthread_t snake_move_tid = 0;

    printf("THread Version starting.....\n");
    //pthread_create(&snake_move_tid, NULL,snake_move, NULL);
    pthread_create(&snake_move_tid, NULL,snake_move_thread1, NULL);
    pthread_join(snake_move_tid, NULL);
}


/*
*@func:在线程中通过复制构造函数复制一份
*/
void test2()
{
    //snake_move_test();
    test_snake_list.push_back(test_snake_1);
    pthread_t snake_move_tid = 0;

    printf("THread Version starting.....\n");
    //pthread_create(&snake_move_tid, NULL,snake_move, NULL);
    pthread_create(&snake_move_tid, NULL,snake_move_thread2, NULL);
    pthread_join(snake_move_tid, NULL);
}

#if 0
void test3()
{
    //snake_move_test();
    test_snake_list_2.push_back(test_snake_1);
    pthread_t snake_move_tid = 0;

    printf("THread Version starting.....\n");
    //pthread_create(&snake_move_tid, NULL,snake_move, NULL);
    pthread_create(&snake_move_tid, NULL,snake_move_thread4, NULL);
    pthread_join(snake_move_tid, NULL);
}
#endif


void show_all_nodes(std::deque<Node> node_list)
{
    for(auto node:node_list)
    {
        std::cout <<"x = "<< node.x <<"\ty = " <<node.y <<std::endl;
    }
}



void snake_move_test()
{
    for(int i=0; i<60; i++)
    {
        test_snake_1.snake_move();
        printf("head's pos[%d, %d]\t snake's len=%d\t", test_snake_1.get_snake_head().x, test_snake_1.get_snake_head().y, test_snake_1.get_snake_len());
        printf("tail's pos[%d, %d]\n", test_snake_1.get_snake_total().back().x, test_snake_1.get_snake_total().back().y);
        sleep(2);
    }
}

/*
*
*/
void* snake_move_thread1(void* arg)
{
    while(1)
    {
        //for(Snake  temp_snake:test_snake_list)//经测试，坐标没有变化
        for(Snake &temp_snake:test_snake_list)//注意看：Snake后加了&
        {
            temp_snake.snake_move();
            printf("head's pos[%d, %d]\t snake's len=%d\t", temp_snake.get_snake_head().x, temp_snake.get_snake_head().y, temp_snake.get_snake_len());
            printf("tail's pos[%d, %d]\n", temp_snake.get_snake_total().back().x, temp_snake.get_snake_total().back().y);
            //处理完所有蛇的数据之后，延时一段时间
            //sleep(1);
        }
        sleep(2);
    }
}


/*
*经测试，坐标没有变化
*/
void* snake_move_thread2(void* arg)
{
    Snake temp_snake(test_snake_2);//这里是复制了一份，而不是传了地址
    while(1)
    {
        //Snake temp_snake(test_snake_2);//在这里蛇头左边不变
        temp_snake.snake_move();
        printf("head's pos[%d, %d]\t snake's len=%d\t", temp_snake.get_snake_head().x, temp_snake.get_snake_head().y, temp_snake.get_snake_len());
        printf("tail's pos[%d, %d]\n", temp_snake.get_snake_total().back().x, temp_snake.get_snake_total().back().y);
        //处理完所有蛇的数据之后，延时一段时间
        //sleep(1);
        
        sleep(2);
    }
}

void* snake_move_thread3(void* arg)
{
    while(1)
    {
        
        test_snake_2.snake_move();
        printf("head's pos[%d, %d]\t snake's len=%d\t", test_snake_2.get_snake_head().x, test_snake_2.get_snake_head().y, test_snake_2.get_snake_len());
        printf("tail's pos[%d, %d]\n", test_snake_2.get_snake_total().back().x, test_snake_2.get_snake_total().back().y);
        //处理完所有蛇的数据之后，延时一段时间
        //sleep(1);
        
        sleep(2);
    }
}

#if 0
void* snake_move_thread4(void* arg)
{
    while(1)
    {
        for(Snake temp_snake:test_snake_list_2)
        {
            temp_snake.snake_move();
            printf("head's pos[%d, %d]\t snake's len=%d\t", temp_snake.get_snake_head().x, temp_snake.get_snake_head().y, temp_snake.get_snake_len());
            printf("tail's pos[%d, %d]\n", temp_snake.get_snake_total().back().x, temp_snake.get_snake_total().back().y);
            //处理完所有蛇的数据之后，延时一段时间
            //sleep(1);
        }
        sleep(2);
    }
}
#endif